package com.jd.finance.core.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jd.finance.core.pojo.entity.AdminApply;
import com.jd.finance.core.service.AdminApplyService;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台账号申请表 前端控制器
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-28
 */
@Api(tags = "添加后台账号")
@Slf4j
@RestController
@RequestMapping("/admin/core/adminApplyCenter")
public class AdminApplyController {

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    @Resource
    private AdminApplyService adminApplyService;

    /**
     *申请账号信息
     */
    @ApiOperation("申请后台账号")
    @PostMapping("/adminAccountApply")
    public BaseResult adminAccountApply(Integer uid, String userPhone, String userPassword, String userName, String userCon, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),userPhone,userPassword,userName,userCon)) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        //判断用户身份
        if (!adminUtilsService.isHR(uid)) {
            log.error("该用户非HR身份!");
            return BaseResult.error().message("该用户非HR身份!");
        }

        return adminApplyService.adminAccountApply(uid,userPhone,userPassword,userName,userCon,request);
    }

    /**
     * 总经理 查询账号申请
     */
    @ApiOperation("总经理 查询账号申请")
    @GetMapping("/getAccountApplyInfo")
    public  BaseResult getAccountApplyInfo(Integer uid,Long page,Long limit) {
        if (uid==null || page==null || limit==null) {
            log.error("信息为空！");
            return BaseResult.error().message("信息为空！");
        }

        if (!adminUtilsService.isGM(uid)) {
            log.error("该用户非总经理身份！");
            return BaseResult.error().message("该用户非总经理身份！");
        }

        Page<AdminApply> pageParam=new Page<>(page,limit);
        Page<AdminApply> pageModel=adminApplyService.getAccountApplyInfo(pageParam,uid);

        List<AdminApply> records=pageModel.getRecords();
        records.stream().filter(bean -> {
            String status=bean.getAdminApplyStatus();
            if (status.equals("0")) {
                bean.setAdminApplyStatus("未审核");
            }else if (status.equals("1") || status.equals("2")) {
                bean.setAdminApplyStatus("已审核");
            }else {
                log.error("信息错误!");
            }
            return true;
        }).collect(Collectors.toList());
        pageModel.setRecords(records);
        return BaseResult.ok().data("pageModel",pageModel);
    }

    /**
     * 根据申请id,查看申请详情
     */
    @ApiOperation("根据申请id,查看申请详情")
    @GetMapping("/getAccountAppInfoById")
    public BaseResult getAccountAppInfoById(Integer uid,Integer accApplyId) {
        if (uid==null || accApplyId==null) {
            log.error("信息为空!");
            return BaseResult.error().message("信息为空!");
        }

        if (!adminUtilsService.isGM(uid)) {
            log.error("该用户非总经理身份!");
            return BaseResult.error().message("该用户非总经理身份!");
        }
        return adminApplyService.getAccountAppInfoById(uid,accApplyId);
    }

    /**
     * 审核账号申请(修改审核状态)
     */
    @ApiOperation("审核账号申请(修改审核状态)")
    @PostMapping("/checkAccountApply")
    public BaseResult checkAccountApply(Integer uid,Integer accAppId,String applyStatus,HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),String.valueOf(accAppId),applyStatus)) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsService.isGM(uid)) {
            log.error("该用户非总经理身份!");
            return BaseResult.error().message("该用户非总经理身份!");
        }
        return adminApplyService.checkAccountApply(uid,accAppId,applyStatus,request);
    }

    /**
     * 添加后台账号
     */
    @ApiOperation("添加后台账号")
    @PostMapping("/addAdminAccount")
    public BaseResult addAdminAccount(Integer uid,Integer accAppId,HttpServletRequest request) {
        if (uid==null || accAppId==null) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsService.isHR(uid)) {
            log.error("该用户非HR身份！");
            return BaseResult.error().message("该用户非HR身份！");
        }

        return adminApplyService.addAdminAccount(uid,accAppId,request);
    }
}

